package com.zhuss.order.processor;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import com.github.wxpay.sdk.WXPay;
import com.github.wxpay.sdk.WXPayUtil;
import com.zhuss.WxCertParam;
import com.zhuss.bean.bo.order.order.WxCareOrderBo;
import com.zhuss.bean.vo.order.order.WxCareOrderVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.IsEnum;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.util.JsonUtil;

/**
 * 微信支付订单查询
 *  processor
 * @Author: zhushanshuo
 * @Date: 2019/9/24 19:54
 */
@Component("wxCareOrderProcessor")
public class WxCareOrderProcessor extends AbstractProcessor<WxCareOrderVo, WxCareOrderBo> implements Processor<WxCareOrderVo, WxCareOrderBo> {

	@Value("${wxpay.appId}")
    private String appId;
	
	@Value("${wxpay.mchId}")
    private String mchId;
	
	@Value("${wxpay.key}")
    private String key;
	
	@Value("${wxpay.tradeType}")
	private String tradeType;
	
	@Value("${wxpay.notifyUrl}")
	private String notifyUrl;
	
    
    /**
     * 校验
     */
    @Override
    public void check(WxCareOrderBo bo) throws DzcxException {
    }
    
    /**
     * 操作方法
     * @throws Exception 
     */
    @Override
    public WxCareOrderVo process(WxCareOrderBo param) throws DzcxException {
    	
    	WxCareOrderVo result = new WxCareOrderVo();
    	
    	param.buildAppId(appId).buildMchId(mchId).buildKey(key);
    	
        WxCertParam wxCertParam = new WxCertParam(param.getAppId(), param.getMchId(), param.getKey(), IsEnum.否.key(), null);
        WXPay wxPay = new WXPay(wxCertParam);

        //根据微信支付的api来设置
        Map<String,String> data = new HashMap<>();
        data.put("appid",param.getAppId());
        data.put("mch_id",param.getMchId());         //商户号
        data.put("out_trade_no", param.getOrderNo());   //交易号
        data.put("nonce_str", param.getNonceStr());   // 随机字符串小于32位
        String sign = null;
        
		try {
			sign = WXPayUtil.generateSignature(data, wxCertParam.getKey());
		} catch (Exception e) {
			e.printStackTrace();
			throw new DzcxException(ExceptionCode.微信支付获取签名错误);
		}  
		
		//签名
        data.put("sign",sign);

        Map<String, String> respData = null;
		try {
			respData = wxPay.orderQuery(data);
		} catch (Exception e) {
			e.printStackTrace();
			throw new DzcxException(ExceptionCode.微信订单查询错误);
		}
        if (respData.get("return_code").equals("SUCCESS")){
            BeanUtils.copyProperties(param, result);
            result.setTransactionId(respData.get("transaction_id"));
            result.setTradeState(respData.get("trade_state"));
            result.setOrderNo(respData.get("out_trade_no"));
            return result;
        }
        return result;
    }
    
    /**
     * 操作日志
     */
	@Override
	public WxCareOrderVo log(WxCareOrderBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.订单.key());
    	record.setAction(LogActionEnum.查询支付单.key());
    	record.setTargetId(param.getTargetId());
    	record.setTargetName(param.getOrderNo());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());
    	
    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}
}
